﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using GradeManager.ServiceReference1;
using GradeManager.classes;

namespace GradeManager
{
    public partial class MainPage : UserControl
    {
        int roleState = 0;
        int index;
        public static List<Student> allStudents = new List<Student>();
        public static List<Teacher> allTeachers = new List<Teacher>();
        public static List<Manager> allManagers = new List<Manager>();
        public static List<Course> allCourses = new List<Course>();
        public static List<Score> scoreList = new List<Score>();

        public MainPage()
        {
            InitializeComponent();
            loadData();

        }

        public void loadData()
        {
            Service1Client client = new Service1Client();

            client.LoadStuXMLAsync("student");
            client.LoadStuXMLCompleted += new EventHandler<LoadStuXMLCompletedEventArgs>(client_LoadStuXMLCompleted);
            client.LoadTeaXMLAsync("teacher");
            client.LoadTeaXMLCompleted += new EventHandler<LoadTeaXMLCompletedEventArgs>(client_LoadTeaXMLCompleted);
            client.LoadManXMLAsync("manager");
            client.LoadManXMLCompleted += new EventHandler<LoadManXMLCompletedEventArgs>(client_LoadManXMLCompleted);
            client.LoadCourseXMLAsync("course");
            client.LoadCourseXMLCompleted += new EventHandler<LoadCourseXMLCompletedEventArgs>(client_LoadCourseXMLCompleted);
            client.LoadScoreXMLAsync();
            client.LoadScoreXMLCompleted += new EventHandler<LoadScoreXMLCompletedEventArgs>(client_LoadScoreXMLCompleted);
        }

        void client_LoadManXMLCompleted(object sender, LoadManXMLCompletedEventArgs e)
        {
            string[] tmpList = e.Result.Split(' ');
            for (int i = 0; i < tmpList.Length - 1; i += 2)
            {
                Manager tmp = new Manager(tmpList[i], tmpList[i + 1]);
                allManagers.Add(tmp);
            }
        }

        void client_LoadTeaXMLCompleted(object sender, LoadTeaXMLCompletedEventArgs e)
        {
            string[] tmpList = e.Result.Split(' ');
            for (int i = 0; i < tmpList.Length - 1; i += 2)
            {
                Teacher tmp = new Teacher(tmpList[i], tmpList[i + 1]);
                allTeachers.Add(tmp);
            }
        }

        void client_LoadStuXMLCompleted(object sender, LoadStuXMLCompletedEventArgs e)
        {
            string[] tmpList = e.Result.Split(' ');
            for (int i = 0; i < tmpList.Length - 1; i += 2)
            {
                Student tmp = new Student(tmpList[i], tmpList[i + 1]);
                allStudents.Add(tmp);
            }
        }

        void client_LoadScoreXMLCompleted(object sender, LoadScoreXMLCompletedEventArgs e)
        {
            string[] tmpList = e.Result.Split(' ');
            
            for (int i = 0; i < tmpList.Length-1; i += 3)
            {
                Score tmp = new Score(tmpList[i], tmpList[i+1], Int32.Parse(tmpList[i+2]));
                scoreList.Add(tmp);
            }
        }

        void client_LoadCourseXMLCompleted(object sender, LoadCourseXMLCompletedEventArgs e)
        {
            string[] tmpList = e.Result.Split(' ');
            
            for (int i = 0; i < tmpList.Length-1; i += 4)
            {
                Course tmp = new Course(tmpList[i], tmpList[i + 1], tmpList[i + 2], tmpList[i + 3]);
                allCourses.Add(tmp);
            }
        }

        //学生登录
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            roleState = 1;
            choose1.Begin();
        }

        //教师登录
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            roleState = 2;
            choose1.Begin();
        }

        //教务处登录
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            roleState = 3;
            choose1.Begin();
        }

        //登录
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            bool flag = false;
            if (roleState == 1)
            {
                for (int i = 0; i < allStudents.Count; i++)
                {
                    if (allStudents[i].ID == id_tb.Text && allStudents[i].password == pswd_tb.Password)
                    {
                        index = i;
                        flag = true;
                        //MessageBox.Show("登陆成功");
                        stuLog.Begin();
                        List<showGrade> contentList = new List<showGrade>();
                        for (int j = 0; j < allCourses.Count; j++)
                        {
                            showGrade tmp = new showGrade();
                            tmp.courseName = allCourses[j].name;
                            tmp.teacherName = allCourses[j].teacherID;
                            tmp.credit = allCourses[j].credit;
                            tmp.grade = allStudents[index].getGrade(allCourses[j].ID).ToString();
                            contentList.Add(tmp);
                        }
                        grade.DataContext = contentList;
                    }
                }
                if (!flag)
                {
                    //MessageBox.Show("登录失败");
                    chooseBack.Begin();
                }
            }
            if (roleState == 2)
            {
                for (int i = 0; i < allTeachers.Count; i++)
                {
                    if (allTeachers[i].ID == id_tb.Text && allTeachers[i].password == pswd_tb.Password)
                    {
                        index = i;
                        flag = true;
                        //MessageBox.Show("登陆成功");
                        teaLog.Begin();
                    }
                }
                if (!flag)
                {
                    //MessageBox.Show("登录失败");
                    chooseBack.Begin();
                }
            }
            if (roleState == 3)
            {
                for (int i = 0; i < allManagers.Count; i++)
                {
                    if (allManagers[i].ID == id_tb.Text && allManagers[i].password == pswd_tb.Password)
                    {
                        index = i;
                        flag = true;
                        //MessageBox.Show("登陆成功");
                        ManLog.Begin();
                    }
                }
                if (!flag)
                {
                    //MessageBox.Show("登录失败");
                    chooseBack.Begin();
                }
            }

        }

        //取消
        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            chooseBack.Begin();
        }

        //增加成绩
        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            Score tmp = new Score(stu_id_tb.Text , cou_id_tb.Text, int.Parse(grade_tb.Text));
            if (allTeachers[index].addGrade(tmp))
            {
                scoreBack();
                ChildWindow2 temp = new ChildWindow2("添加成功");
                temp.Show();
            }
            else
            {
                ChildWindow2 temp = new ChildWindow2("添加失败");
                temp.Show();
            }
        }

        //更新成绩
        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            Score tmp = new Score(stu_id_tb.Text, cou_id_tb.Text, int.Parse(grade_tb.Text));
            if (allTeachers[index].updateGrade(tmp))
            {
                scoreBack();
                ChildWindow2 temp = new ChildWindow2("更新成功");
                temp.Show();
            }
            else
            {
                ChildWindow2 temp = new ChildWindow2("更新失败");
                temp.Show();
            }
        }

        private void Button_Click_7(object sender, RoutedEventArgs e)
        {
            if (allManagers[index].sortGrade(id_tb3.Text))
            {
                ChildWindow2 temp = new ChildWindow2("排序成功");
                temp.Show();
            }
            else
            {
                ChildWindow2 tmp = new ChildWindow2("排序失败");
                tmp.Show();
            }
        }

        private void Button_Click_8(object sender, RoutedEventArgs e)
        {
            allManagers[index].selectGood();
        }

        private void Button_Click_9(object sender, RoutedEventArgs e)
        {
            allManagers[index].statistics();
        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            login.Visibility = Visibility.Collapsed;
            student.Visibility = Visibility.Collapsed;
            teacher.Visibility = Visibility.Collapsed;
            manager.Visibility = Visibility.Collapsed;
            back.Begin();
        }


        private void scoreBack()
        {
            for (int i = 0; i < scoreList.Count; i++)
            {
                Service1Client client = new Service1Client();
                client.BackScoreXMLAsync(scoreList[i].courseID, scoreList[i].studentID, scoreList[i].grade.ToString());
            }
        }
    }
}
